package cn.group.hotel2.service.impl;

import cn.group.hotel2.mapper.FinanceMapper;
import cn.group.hotel2.pojo.dto.FinanceGetCountDTO;
import cn.group.hotel2.pojo.dto.FinancePriceDTO;
import cn.group.hotel2.pojo.dto.FinanceSelectDTO;
import cn.group.hotel2.pojo.entity.Finance;
import cn.group.hotel2.pojo.entity.FinanceGetCount;
import cn.group.hotel2.pojo.entity.FinancePrice;
import cn.group.hotel2.pojo.vo.FinanceListItemVO;
import cn.group.hotel2.service.IFinanceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @author : zlh
 * @date : 2022-11-26 10:40
 **/
@Slf4j
@Service
public class FinanceServiceImpl implements IFinanceService {

    @Autowired
    FinanceMapper financeMapper;

    public FinanceServiceImpl() {
        log.debug("创建业务类对象:FinanceServiceImpl");
    }

    @Override
    public List<FinanceListItemVO> list(FinanceSelectDTO financeGetCountDTO) throws ParseException {
        log.debug("开始处理【查询财务列表】的业务，参数:页码大小:{},当前页:{},入住时间:{},离开时间:{}",
                financeGetCountDTO.getPageSize(), financeGetCountDTO.getCurrentPage(),
                financeGetCountDTO.getStartTime(), financeGetCountDTO.getEndTime());
        SimpleDateFormat dateformat;
        Date dateStart = null;
        Date dateEnd = null;
        if (financeGetCountDTO.getStartTime() != null && financeGetCountDTO.getStartTime() != ""
                && financeGetCountDTO.getEndTime() != null && financeGetCountDTO.getEndTime() != "") {
            dateformat = new SimpleDateFormat("yyyy-MM-dd");
            dateStart = dateformat.parse(financeGetCountDTO.getStartTime());
            dateEnd = dateformat.parse(financeGetCountDTO.getEndTime());
        }
        Integer record = (financeGetCountDTO.getCurrentPage() - 1) * financeGetCountDTO.getPageSize();
        Finance finance = new Finance();
        BeanUtils.copyProperties(financeGetCountDTO, finance);
        finance.setRecord(record);
        finance.setStartTime(dateStart);
        finance.setEndTime(dateEnd);
        List<FinanceListItemVO> list = financeMapper.list(finance);
        for (FinanceListItemVO f : list) {
            LocalDateTime checkInTime = f.getOrderStandardVO().getCheckInTime();
            LocalDateTime checkOutTime = f.getOrderStandardVO().getCheckOutTime();
            Duration duration = Duration.between(checkInTime, checkOutTime);
            Long days = duration.toDays();
            f.setDays(days);
        }
        return list;

    }

    @Override
    public Double selectSumPrice(FinancePriceDTO financePriceDTO) throws ParseException {
        log.debug("开始处理【查询财务总收入】的业务,参数:{}",financePriceDTO);
        FinancePrice financePrice = new FinancePrice();
        BeanUtils.copyProperties(financePriceDTO,financePrice);
        Date dateStart = null;
        Date dateEnd = null;
        if (financePriceDTO.getStartTime() != null && financePriceDTO.getStartTime() != ""
                && financePriceDTO.getEndTime() != null && financePriceDTO.getEndTime() != "") {

            SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
            dateStart = dateformat.parse(financePriceDTO.getStartTime());
            dateEnd = dateformat.parse(financePriceDTO.getEndTime());
        }
        financePrice.setStartTime(dateStart);
        financePrice.setEndTime(dateEnd);
        return financeMapper.selectSumPrice(financePrice);
    }

    @Override
    public Integer getCount(FinanceGetCountDTO financeGetCountDTO) throws ParseException {
        log.debug("开始处理【获取订单数量】的业务,参数:{}",financeGetCountDTO);
         FinanceGetCount financeGetCount  = new FinanceGetCount();
        BeanUtils.copyProperties(financeGetCountDTO,financeGetCount);
        Date dateStart = null;
        Date dateEnd = null;
        if (financeGetCountDTO.getStartTime() != null && financeGetCountDTO.getStartTime() != ""
                && financeGetCountDTO.getEndTime() != null && financeGetCountDTO.getEndTime() != "") {

            SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
            dateStart = dateformat.parse(financeGetCountDTO.getStartTime());
            dateEnd = dateformat.parse(financeGetCountDTO.getEndTime());
        }
        financeGetCount.setStartTime(dateStart);
        financeGetCount.setEndTime(dateEnd);
        Integer count = financeMapper.count(financeGetCount);
        return count;
    }

}
